package com.xs;

import java.io.Serializable;
import java.util.Enumeration;
import java.util.Optional;
import java.util.stream.Stream;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class DemoApplication {

	private Log log = LogFactory.getLog(DemoApplication.class);

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

	@GetMapping("/helloworld/hello")
	public String hello() {
		log.info("============================分割线/helloworld/hello=================================");
		return "hello";
	}

	@GetMapping("/helloworld//world")
	public String world() {
		log.info("============================分割线/helloworld//world=================================");
		return "world";
	}
	
	
	@GetMapping("/hello")
	public String hello2() {
		try {
			Thread.currentThread().sleep(30*1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		log.info("============================分割线/hello=================================");
		return "hello";
	}

	@GetMapping("/world")
	public String world2() {
		log.info("============================分割线/world=================================");
		return "world";
	}


	@PostMapping("/users/getUser")
	public User user(@RequestBody  User user, HttpServletRequest request) {
		Enumeration<String> v = request.getHeaderNames();
		while (v.hasMoreElements()) {
			log.info(v.nextElement());
		}

		log.info("============================分割线/users/getUser=================================");

		if (request.getCookies() != null) {
			Stream.of(request.getCookies()).forEach(e -> {
				log.info(e.getName());
			});
		}

		user.setUserName("lisi");
		return user;
	}
	
	
	@PostMapping("/getUser")
	public User user2(@RequestBody User user, HttpServletRequest request) {
		Enumeration<String> v = request.getHeaderNames();
		while (v.hasMoreElements()) {
			log.info(v.nextElement());
		}

		
		log.info("============================分割线/getUser=================================");

		if (request.getCookies() != null) {
			Stream.of(request.getCookies()).forEach(e -> {
				log.info(e.getName());
			});
		}

		user.setUserName("lisi");
		try {
			Thread.currentThread().sleep(30*1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	}


}
